﻿Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Security.Permissions
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules.Design
    Friend Class RuleSetPropertyDescriptor
        Inherits RuleDefinitionDynamicPropertyDescriptor
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal descriptor As PropertyDescriptor)
            MyBase.New(serviceProvider, descriptor)
        End Sub

        Public Overrides Function GetEditor(ByVal editorBaseType As Type) As Object
            New SecurityPermission(PermissionState.Unrestricted).Demand
            Return New RuleSetDefinitionEditor
        End Function

        Public Overrides Function GetValue(ByVal component As Object) As Object
            If (component Is Nothing) Then
                Throw New ArgumentNullException("component")
            End If
            Dim reference As RuleSetReference = [TryCast](component,RuleSetReference)
            If Not String.IsNullOrEmpty(reference.RuleSetName) Then
                Dim ruleDefinitions As RuleDefinitions = MyBase.GetRuleDefinitions(component)
                If (Not ruleDefinitions Is Nothing) Then
                    Dim ruleSets As RuleSetCollection = ruleDefinitions.RuleSets
                    If ((Not ruleSets Is Nothing) AndAlso ruleSets.Contains(reference.RuleSetName)) Then
                        Return ruleSets.Item(reference.RuleSetName)
                    End If
                End If
            End If
            Return Nothing
        End Function

        Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
            If (component Is Nothing) Then
                Throw New ArgumentNullException("component")
            End If
            Dim reference As RuleSetReference = [TryCast](component,RuleSetReference)
            If (reference Is Nothing) Then
                Throw New ArgumentNullException("component")
            End If
            Dim item As RuleSet = [TryCast](value,RuleSet)
            If Not String.IsNullOrEmpty(reference.RuleSetName) Then
                Dim serviceProvider As ISite = PropertyDescriptorUtils.GetSite(MyBase.ServiceProvider, component)
                If (serviceProvider Is Nothing) Then
                    Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.MissingService, New Object() { GetType(ISite).FullName }))
                End If
                Dim ruleSets As RuleSetCollection = Nothing
                Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(serviceProvider, Helpers.GetRootActivity([TryCast](serviceProvider.Component,Activity)))
                If (Not definitions Is Nothing) Then
                    ruleSets = definitions.RuleSets
                End If
                If ((Not ruleSets Is Nothing) AndAlso ruleSets.Contains(reference.RuleSetName)) Then
                    ruleSets.Remove(reference.RuleSetName)
                    ruleSets.Add(item)
                    ConditionHelper.Flush_Rules_DT(serviceProvider, Helpers.GetRootActivity([TryCast](serviceProvider.Component,Activity)))
                End If
            End If
        End Sub


        ' Properties
        Public Overrides ReadOnly Property Converter As TypeConverter
            Get
                Return New RuleSetDefinitionTypeConverter
            End Get
        End Property

        Public Overrides ReadOnly Property Description As String
            Get
                Return SR.GetString("RuleSetDefinitionDescription")
            End Get
        End Property

        Public Overrides ReadOnly Property IsReadOnly As Boolean
            Get
                Return False
            End Get
        End Property

    End Class
End Namespace

